package com.shangjinrong.controller.site;

import com.shangjinrong.domain.BorrowInfo;
import com.shangjinrong.result.R;
import com.shangjinrong.service.BorrowInfoService;
import com.shangjinrong.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@CrossOrigin
@RequestMapping("site/borrowInfo")
public class SiteBorrowInfoController {
    @Autowired
    private BorrowInfoService borrowInfoService;

    @GetMapping("getBorrowAmount")
    public R getBorrowAmount(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        return R.ok().data("borrowAmount", borrowInfoService.getBorrowAmount(userId));
    }

    @PostMapping("save")
    public R save(@RequestBody BorrowInfo borrowInfo, HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        borrowInfoService.saveBorrowInfo(borrowInfo, userId);
        return R.ok().message("提交成功");
    }

    @GetMapping("getBorrowInfoStatus")
    public R getBorrowInfoStatus(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        return R.ok().data("status",borrowInfoService.getStatusByUserId(userId));
    }
}
